package net.lab1024.sa.admin.module.users.inventorycheck.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.lab1024.sa.admin.module.fabricmaterials.dao.FabricMaterialsDao;
import net.lab1024.sa.admin.module.fabricmaterials.domain.entity.FabricMaterialsEntity;
import net.lab1024.sa.admin.module.users.inventorycheck.dao.InventoryCheckDao;
import net.lab1024.sa.admin.module.users.inventorycheck.domain.entity.InventoryCheckEntity;
import net.lab1024.sa.admin.module.users.inventorycheck.domain.form.InventoryCheckAddForm;
import net.lab1024.sa.admin.module.users.inventorycheck.domain.form.InventoryCheckQueryForm;
import net.lab1024.sa.admin.module.users.inventorycheck.domain.form.InventoryCheckUpdateForm;
import net.lab1024.sa.admin.module.users.inventorycheck.domain.vo.InventoryCheckVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 库存盘点 Service
 *
 * @Author lyy
 * @Date 2024-12-10 23:31:13
 * @Copyright lyy
 */

@Service
public class InventoryCheckService {

    @Resource
    private InventoryCheckDao inventoryCheckDao;

    @Resource
    private FabricMaterialsDao fabricMaterialsDao;
    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<InventoryCheckVO> queryPage(InventoryCheckQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<InventoryCheckVO> list = inventoryCheckDao.queryPage(page, queryForm);
        PageResult<InventoryCheckVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(InventoryCheckAddForm addForm) {
        InventoryCheckEntity inventoryCheckEntity = SmartBeanUtil.copy(addForm, InventoryCheckEntity.class);
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        Timestamp timestamp = Timestamp.valueOf(format);
        inventoryCheckEntity.setCheckDate(timestamp);
        int insert = inventoryCheckDao.insert(inventoryCheckEntity);
        if (insert <= 0) {
            return ResponseDTO.ok("添加失败");
        }
        FabricMaterialsEntity fabricMaterials = new FabricMaterialsEntity();
        fabricMaterials.setId(inventoryCheckEntity.getStockId());
        fabricMaterials.setLastCheckQty(inventoryCheckEntity.getCheckQuantity());
        fabricMaterials.setLastCheckDate(inventoryCheckEntity.getCheckDate());
        fabricMaterialsDao.updateById(fabricMaterials);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(InventoryCheckUpdateForm updateForm) {
        InventoryCheckEntity inventoryCheckEntity = SmartBeanUtil.copy(updateForm, InventoryCheckEntity.class);
        inventoryCheckDao.updateById(inventoryCheckEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        inventoryCheckDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        inventoryCheckDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
